package com.bmob.lover.data.bindadapter

import android.text.TextUtils
import android.view.View
import android.widget.CheckBox
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.theone.common.ext.getPriceSpannableString
import com.bmob.lover.data.constant.AnniversaryConstant
import com.theone.common.ext.YYYY_MM_DD_HH_MM_SS
import com.theone.common.ext.formatLong
import com.theone.common.util.DateFormatUtils

object CustomBindAdapter {

    @BindingAdapter(value = ["title", "detail"], requireAll = false)
    @JvmStatic
    fun QMUICommonListItemView(
        itemView: QMUICommonListItemView,
        title: String?,
        detail: String?
    ) {
        itemView.text = title
        itemView.detailText = detail
    }

    @BindingAdapter("anniversaryType")
    @JvmStatic
    fun AnniversaryType(
        textView: TextView,
        type: Int
    ) {
        textView.text = AnniversaryConstant.TYPE_ITEMS[type]
    }

    @BindingAdapter("price")
    @JvmStatic
    fun priceMoney(
        textView: TextView,
        price: Double
    ) {
        textView.text  = getPriceSpannableString(textView.context,price)
    }

    @BindingAdapter(value = ["gone"], requireAll = false)
    @JvmStatic
    fun goneNoEmpty(view: View, text: String) {
        view.visibility = if (TextUtils.isEmpty(text)) View.VISIBLE else View.GONE
    }

    @BindingAdapter(value = ["invisible"], requireAll = false)
    @JvmStatic
    fun invisibleNoEmpty(view: View, text: String) {
        view.visibility = if (TextUtils.isEmpty(text)) View.INVISIBLE else View.VISIBLE
    }

    /**
     * 格式化时间
     * @param textView TextView
     * @param longDate Long 格式的时间
     * @param type Int? 类型
     */
    @BindingAdapter(value = ["date", "type"], requireAll = false)
    @JvmStatic
    fun formatDate(
        textView: TextView,
        date: String,
        type: Int?
    ) {
        date.formatLong(YYYY_MM_DD_HH_MM_SS)?.let {
            textView.text = DateFormatUtils.formatTimeStampString(
                textView.context,
                it,
                type ?: DateFormatUtils.FORMAT_TYPE_PERSONAL_FOOTPRINT
            )
        }

    }

    @BindingAdapter(value = ["check"])
    @JvmStatic
    fun checkBox(view: CheckBox, check: Boolean) {
        view.isChecked = check
    }

}